import {Component, OnInit, ViewChild} from '@angular/core';
import {ModalDirective} from 'ngx-bootstrap';
import {adSystemApiUrl, AliyunOssOutputUrl, tpType} from '../../../../config/api';
import {HttpService} from '../../../lib/service/http.service';
import {Http, Headers, RequestOptions, RequestOptionsArgs} from '@angular/http';
import {LocalStorageService} from '../../../lib/service/local-storage.service';

@Component({
  selector: 'app-setting',
  templateUrl: 'setting.component.html',
  styleUrls: ['setting.component.scss']
})
export class SettingComponent implements OnInit {

  applet_id;
  applet_uuid;
  coupons_key;
  tuike;
  modifiedType = 'basic';
  userList;
  wechatid;
  access_token;
  AppID;
  appletName;
  mch_id;
  pay_key;
  apiclient_cert;
  apiclient_key;

  type: string;
  message: string;
  isShow: boolean;

  @ViewChild('addTesterModal') public addTesterModal: ModalDirective;

  constructor(private httpService: HttpService, private localStorageService: LocalStorageService) {
  }

  ngOnInit() {
    this.applet_id = this.localStorageService.getNowApplet().id;
    this.applet_uuid = this.localStorageService.getNowApplet().applet_uuid;
    this.AppID = this.localStorageService.getNowApplet().AppID;
    this.appletName = this.localStorageService.getNowApplet().nick_name;
    this.getCouponStatus();
    this.getBindTesterList();
    this.getPayment();
  }

  // 获取小程序卡券开通状态
  getCouponStatus() {
    const applet = this.localStorageService.getNowApplet();
    applet.applet_extend.every((val, idx, array) => {
      if (val.type === 1) {
        this.coupons_key = val.status;
      } else if (val.type === 2) {
        this.tuike = val.status;
      }
      return true;
    });
  }

  // 获取小程序详情
  getAppletInfo() {
    const url = `${adSystemApiUrl}/v1/member/applets/applet_detail`;
    return this.httpService.httpGetAuth(url).subscribe(res => {
      this.localStorageService.setNowApplet(res.data);
      this.getCouponStatus();
    });
  }

  // 绑定的测试用户列表
  getBindTesterList() {
    const url = `${adSystemApiUrl}/v1/member/wechat/bind_tester_list?applet_uuid=${this.applet_uuid}&page=1&pageSize=999`;
    return this.httpService.httpGetAuth(url).subscribe(
      res => {
        if (res.code === 2000) {
          this.userList = res.data.data;
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  // 获取支付信息
  getPayment() {
    const url = `${adSystemApiUrl}/v1/member/applets/getPaymentSetting?applet_id=${this.applet_id}&applet_uuid=${this.applet_uuid}`;
    return this.httpService.httpPostAuth(url, '').subscribe(
      res => {
        if (!!res.data) {
          this.pay_key = res.data.pay_key;
          this.mch_id = res.data.mch_id;
          this.apiclient_cert = res.data.apiclient_cert;
          this.apiclient_key = res.data.apiclient_key;
        }
      }
    );
  }

  // 解绑用户
  unbindTester(wechatid) {
    // const data = {applet_uuid: this.applet_uuid,access_token: this.access_token,wechatid: wechatid};
    const data = {wechatid: wechatid};
    const url = `${adSystemApiUrl}/v1/member/wechat/unbind_tester`;
    return this.httpService.httpPostAuth(url, data).subscribe(
      res => {
        if (res.code === 2000) {
          this.type = 'success';
          this.message = '解绑成功';
          this.isShow = true;
          this.getBindTesterList();
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  // 增加测试用户
  addTester() {
    // const data = {applet_uuid: this.applet_uuid, access_token: this.access_token, wechatid: this.wechatid};
    const data = {wechatid: this.wechatid};
    const url = `${adSystemApiUrl}/v1/member/wechat/bind_tester`;
    return this.httpService.httpPostAuth(url, data).subscribe(
      res => {
        if (res.code === 2000) {
          this.type = 'success';
          this.message = '新增测试用户成功';
          this.isShow = true;
          this.addTesterModal.hide();
          this.getBindTesterList();
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  // 绑定支付系统
  bindPaySubmit() {
    this.mch_id = this.mch_id.replace(/\s+/g, '');
    this.pay_key = this.pay_key.replace(/\s+/g, '');
    const url = `${adSystemApiUrl}/v1/member/applets/payment_setting`;
    const data = {applet_id: this.applet_id, applet_uuid: this.applet_uuid, mch_id: this.mch_id, pay_key: this.pay_key, apiclient_cert: this.apiclient_cert, apiclient_key: this.apiclient_key};
    return this.httpService.httpPostAuth(url, data).subscribe(
      res => {
        if (res.code === 2000) {
          this.type = 'success';
          this.message = '保存成功';
          this.isShow = true;
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  // 申请卡券key
  applyKey() {
    const url = `${adSystemApiUrl}/v2/member/account/apply_merchant_key_post`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      name: this.appletName,
      tp_id: this.applet_uuid,
      tp_type: tpType
    });
    return this.httpService.httpPostAuth(url, body, options).subscribe(res => {
      if (res.code === 2000) {
        this.getAppletInfo();
        this.type = 'success';
        this.message = '卡券开通成功';
        this.isShow = true;
      } else {
        this.type = 'error';
        this.message = res.message;
        this.isShow = true;
      }
    });
  }

  // 关闭卡券功能
  closeCoupon() {
    const url = `${adSystemApiUrl}/v2/member/account/close_coupons`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      applet_uuid: this.applet_uuid
    });
    return this.httpService.httpPostAuth(url, body, options).subscribe(res => {
      if (res.code === 2000) {
        this.getAppletInfo();
        this.type = 'success';
        this.message = '关闭成功';
        this.isShow = true;
      } else {
        this.type = 'error';
        this.message = res.message;
        this.isShow = true;
      }
    });
  }

  // 申请推客开通
  applyTuike() {
    const url = `${adSystemApiUrl}/v2/member/tuike/account/apply`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    console.log(2222);
    const body = JSON.stringify({
      name: this.appletName,
      tp_id: this.applet_uuid,
      tp_type: tpType
    });
    return this.httpService.httpPostAuth(url, body, options).subscribe(res => {
      if (res.code === 2000) {
        this.getAppletInfo();
        this.type = 'success';
        this.message = '推客开通成功';
        this.isShow = true;
      } else {
        this.type = 'error';
        this.message = res.message;
        this.isShow = true;
      }
    });
  }

  // 关闭推客功能
  closeTuike() {
    this.type = 'info';
    this.message = '推客功能不允许关闭';
    this.isShow = true;
  }
}
